$(function () {
	loadEstateTree();
	var treeObj;
	var clickNode;
});

function loadEstateTree(){
	var url = jypath +'/estate/treeComponent/findEstateTree';
	$.ajax({
	    type: "POST",
        url: url,
        data: {id:"project_"+projectId,other:"1"},
        dataType: "json",
        success: function(data){
        	//设置
    		$.fn.zTree.init(
    			$("#estateTreeComponent"),{
    				view:{
    					selectedMulti:false,
    					fontCss:{color:"#393939"}
    			    },
    			    async: {
    					enable: true,
    					url:jypath +'/estate/treeComponent/findEstateTree',
    					dataType: "json",
    					autoParam:["id","other"],
    					dataFilter: ajaxDataFilter
    				},
    				data:{
    				   simpleData: {enable: true}
    			    },callback:{
    			    	onClick:estateTreeClick
    			    }
    			},
    			data.obj
    	    );
    		//加载跟节点
    		treeObj = $.fn.zTree.getZTreeObj("estateTreeComponent");
    		var nodes = treeObj.getNodes();
    		if(nodes.length>0){
    			//默认选中第一个
    			treeObj.selectNode(nodes[0]);
    			estateTreeClick(null,null,nodes[0]);
    		}
        }
	});
}

function ajaxDataFilter(treeId, parentNode, responseData) {
    return responseData.obj;
};

function estateTreeClick(event,treeId,treeNode){
	$("#rightContentDiv").html("");
	var id = treeNode.id;
	var other = treeNode.other;
	var detailUrl = treeNode.detailUrl;
	var menu = $("#menuId").val();
	if(id){
		var loadIndex = layer.load(0, {shade: false});
		clickNode = treeNode;
		id = id.substring(id.indexOf("_")+1);
		var url = jypath + detailUrl + '?id=' + id+'&menu='+menu;
		$.post(url, null, function(data){
			layer.close(loadIndex);
			$("#rightContentDiv").html(data);
		});
	}
}

function deleteNode(){
	treeObj.removeNode(clickNode);
}

